package com.example.administrator.myapplication.activity.albumActivity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.activity.albumActivity.bean.programEntity.Album;
import com.example.administrator.myapplication.activity.albumActivity.bean.programEntity.ProgramEntity;
import com.example.administrator.myapplication.activity.albumActivity.fragment.ContentFragment;
import com.example.administrator.myapplication.activity.albumActivity.fragment.ProgramFragment;
import com.example.administrator.myapplication.callback.XmlyService;
import com.example.administrator.myapplication.databinding.ActivityAlbumBinding;
import com.example.administrator.myapplication.until.BaseApplication;
import com.example.administrator.myapplication.until.CommentFragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AlbumActivity extends AppCompatActivity implements Callback<ProgramEntity> {
     private ActivityAlbumBinding  binding;
    private XmlyService service;
    private  int tracks;
    private  int albumId;
    private Album album;
    private  ContentFragment contentFragment;
    private String titles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_album);
        aboutIntentExtra();
        aboutRelativeLayout();
    }

    private void aboutIntentExtra() {
        Intent intent = getIntent();
        tracks =  intent.getIntExtra("tracks",0);
        albumId = intent.getIntExtra("albumId",0);
    }

    private void aboutRelativeLayout() {
        service = ((BaseApplication) getApplicationContext()).getService();
        service.getProgramEntity(albumId,20).enqueue(this);
    }

    private void aboutTablayout() {
        String title[]  = new String[]{"详情","节目("+tracks+")"};
        List<Fragment> fragments = new ArrayList<>();
      contentFragment = ContentFragment.newInstance(album);
        ProgramFragment programFragment = ProgramFragment.newInstance(albumId);
        fragments.add(contentFragment);
        fragments.add(programFragment);
        CommentFragmentPagerAdapter adapter = new CommentFragmentPagerAdapter(getSupportFragmentManager(),title,fragments);
        binding.pager.setAdapter(adapter);
        binding.tablayout.setupWithViewPager(binding.pager);
        binding.pager.setCurrentItem(1);
    }

    @Override
    public void onResponse(Call<ProgramEntity> call, Response<ProgramEntity> response) {
        album = response.body().getData().getAlbum();
        aboutTablayout();
        binding.setAlbum(response.body().getData().getAlbum());
    }

    @Override
    public void onFailure(Call<ProgramEntity> call, Throwable t) {
        Toast.makeText(this, "albumAcyivity"+t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
